package com.chenyun.system.generator.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.chenyun.system.generator.model.GenDatasourceStore;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * jdbc template获取
 * @author Lonni
 */
public class JdbcTemplateUtil {

    /**
     * template获取
     * @param store
     * @return
     */
    public static JdbcTemplate getJdbcTemplate(GenDatasourceStore store){
        JdbcTemplate jdbcTemplate=new JdbcTemplate(druidDataSource(store));
        return jdbcTemplate;
    }

    private  static DruidDataSource druidDataSource(GenDatasourceStore store){
        DruidDataSource dataSource = new DruidDataSource();
       // dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/ry_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true");
        dataSource.setUrl(store.getUrl());
        dataSource.setUsername(store.getUserName());
        dataSource.setPassword(store.getPwd());
        dataSource.setDriverClassName(store.getDriverName());
        dataSource.setInitialSize(5);//初始化时建立物理连接的个数
        dataSource.setMaxActive(20);//最大连接池数量
        dataSource.setMinIdle(0);//最小连接池数量
        dataSource.setMaxWait(50000);//获取连接时最大等待时间，单位毫秒。
        dataSource.setValidationQuery("SELECT 1");//用来检测连接是否有效的sql
        return dataSource;
    }

}
